//
//  MoreReplyView.m
//  Fireball
//
//  Created by 俞渊华 on 2017/12/18.
//  Copyright © 2017年 俞渊华. All rights reserved.
//

#import "MoreReplyView.h"
#import "ZInputToolbar.h"
#import "CornerView.h"
#import "CommentSectionView.h"

@interface MoreReplyView ()<UITableViewDelegate,UITableViewDataSource,ZInputToolbarDelegate>
@property(nonatomic, strong)CornerView *contentView;

@property(nonatomic, strong)UIView *headerView;
@property(nonatomic, strong)UIButton *cancelButton;
@property(nonatomic, strong)UILabel *titleLabel;
@property(nonatomic, strong)UIView *lineView;

///主评论
@property(nonatomic, strong)CommentSectionView *mainComment;
@property(nonatomic, strong)UILabel *subTitleLabel;


@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)UIView *toolView;
@property(nonatomic, strong)UIView *toolLine;
@property(nonatomic, strong)UIButton *commentBtn;
@property(nonatomic, strong)UIButton *shareButton;
@property(nonatomic, strong)ZInputToolbar *inputToolbar;

@property(nonatomic, strong)AticleComment *commentData;

@property(nonatomic, assign)BOOL commentSub;
@property(nonatomic, strong)AticleComment *commentModel;
@property(nonatomic, strong)NSMutableDictionary *commentDict;

@end

@implementation MoreReplyView

-(instancetype)initWithFrame:(CGRect)frame{
    if ([super initWithFrame:frame]) {
        self.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT);
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
        self.contentView = [[CornerView alloc] init];
        self.contentView.backgroundColor = [UIColor whiteColor];
        self.contentView.radius = 10;
        self.contentView.xm_corner = UIRectCornerTopLeft|UIRectCornerTopRight;
        [self addSubview:self.contentView];
        

        
        self.headerView = [[UIView alloc] init];
        [self.contentView addSubview:self.headerView];
        
        self.cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.cancelButton setImage:[UIImage imageNamed:@"filter_delete"] forState:UIControlStateNormal];
        [self.cancelButton addTarget:self action:@selector(cancelButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [self.headerView addSubview:self.cancelButton];
        
        self.titleLabel = [[UILabel alloc] init];
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.titleLabel.font = [UIFont xm_font:17.0];
        self.titleLabel.textColor = [UIColor xm_jetblackColor];
        [self.headerView addSubview:self.titleLabel];
        
        self.lineView = [[UIView alloc] init];
        self.lineView.backgroundColor = [UIColor xm_splitlineColor];
        [self.headerView addSubview:self.lineView];

        
        
        self.mainComment = [[CommentSectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 108)];
        [self.contentView addSubview:self.mainComment];
        
        self.subTitleLabel = [[UILabel alloc] init];
        self.subTitleLabel.textColor = [UIColor xm_jetblackColor];
        self.subTitleLabel.font = [UIFont xm_medium:17.0];
        self.subTitleLabel.text = @"全部评论";
        [self.contentView addSubview:self.subTitleLabel];
        
        
        [self.contentView addSubview:self.tableView];
        
        
        
        self.toolView = [[UIView alloc] init];
        [self.contentView addSubview:self.toolView];
        
        self.toolLine = [[UIView alloc] init];
        self.toolLine.backgroundColor = [UIColor xm_splitlineColor];
        [self.toolView addSubview:self.toolLine];
        
        self.commentBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.commentBtn.backgroundColor = [UIColor xm_colorFromRGB:0xF7F8FA];
        [self.commentBtn setTitle:@" 添加评论..." forState:UIControlStateNormal];
        [self.commentBtn setTitleColor:[UIColor xm_somberColor] forState:UIControlStateNormal];
        self.commentBtn.titleLabel.font = [UIFont xm_font:13.0];
        self.commentBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        self.commentBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 0);
        self.commentBtn.clipsToBounds = YES;
        self.commentBtn.layer.cornerRadius = 33.0 /2;
        [self.commentBtn addTarget:self action:@selector(commentButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [self.toolView addSubview:self.commentBtn];
        
        self.shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.shareButton setImage:[UIImage imageNamed:@"filter_delete"] forState:UIControlStateNormal];
        [self.shareButton addTarget:self action:@selector(shareButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [self.toolView addSubview:self.shareButton];
        

        
    }
    return self;
}
-(void)setComment:(AticleComment *)comment{
    _comment = comment;
    self.titleLabel.text = [NSString stringWithFormat:@"%lu条回复",(unsigned long)_comment.CommentChildrens.count];
    self.mainComment.comment = _comment;
    [self reloadComment];

}
-(void)reloadComment{
    NSDictionary * dict = @{@"ArticleID":@(self.comment.ArticleID),
                            @"CommentID":@(self.comment.CommentID),
                            @"PageInfo":@{
                                           @"CurrentPage":@(1),
                                           @"PageSize":@(10)
                                         }
                            };
    
    [FBNetworkTool get:@"articleapi/com.GetAticleCommentInfoChildren" parameters:dict showHUD:YES success:^(XMHttpResponseModel *responseModel) {
        NSLog(@"responseModel == %@",responseModel);
        self.commentData = [AticleComment mj_objectWithKeyValues:responseModel.data];
        [self.tableView reloadData];
    } failure:nil];
}
#pragma mark -
-(void)commentButtonClick{
    NSLog(@"commentButtonClick");
    [self.inputToolbar.textInput becomeFirstResponder];
}
-(void)shareButtonClick{
    NSLog(@"shareButtonClick");
}
-(void)cancelButtonClick{
    [UIView animateWithDuration:0.24 animations:^{
        self.y = SCREEN_HEIGHT;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

-(void)replySubCommentWithModel:(AticleComment *)model{
    self.commentModel = model;
    self.commentSub = YES;
    [self.inputToolbar.textInput becomeFirstResponder];
    
//    [self.inputToolbar sendSuccessEndEditing];
    
}
#pragma mark -

-(void)inputToolbar:(ZInputToolbar *)inputToolbar sendContent:(NSString *)sendContent {
    // 清空输入框文字
    if (sendContent.length == 0) {
        return;
    }
    
    [self.commentDict setObject:@(self.comment.ArticleID) forKey:@"ArticleID"];

    if (self.commentSub) {
        [self.commentDict setObject:@(self.commentModel.CommentID) forKey:@"CommentID"];
        [self.commentDict setObject:[NSString stringWithFormat:@"%@||%@",sendContent,self.commentModel.NikeName] forKey:@"Content"];
    }else{
        [self.commentDict setObject:@(self.comment.CommentID) forKey:@"CommentID"];
        [self.commentDict setObject:sendContent forKey:@"Content"];
    }

    [FBNetworkTool get:@"articleapi/com.AddCommontInfo" parameters:self.commentDict showHUD:YES success:^(XMHttpResponseModel *responseModel) {
        if (responseModel.code == 200) {
            [inputToolbar sendSuccessEndEditing];
            
            ///请求成功重新请求评论列表
            [self reloadComment];
        }
    } failure:^(NSError *error) {
        
    }];

}

#pragma mark -

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.commentData.CommentChildrens.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    AticleComment * model = self.commentData.CommentChildrens[section];
    CGFloat mainCommentH = [model.Content xm_sizeWithFont:kCommentFont maxW:self.width - 80].height;
    return mainCommentH + 50 + 40;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 0;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    static NSString *viewIdentfier = @"commentHeaderView";
    CommentSectionView *sectionHeadView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:viewIdentfier];
    if(!sectionHeadView){
        sectionHeadView = [[CommentSectionView alloc] initWithReuseIdentifier:viewIdentfier];
        sectionHeadView.commentType = 110;
    }
    WEAKSELF
    sectionHeadView.block = ^(AticleComment *model) {
        [weakSelf replySubCommentWithModel:model];
    };
    sectionHeadView.comment = self.commentData.CommentChildrens[section];
    return sectionHeadView;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * identifier = @"identifier";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}



-(void)layoutSubviews{
    [super layoutSubviews];
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_equalTo(self);
        make.top.mas_equalTo(self).offset(STATUSBARHEIGHT);
    }];
    
    [self.toolView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_equalTo(self.contentView);
        make.height.mas_equalTo(kTabBarHeight);
    }];
    [self.toolLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(self.toolView);
        make.height.mas_equalTo(@0.5);
    }];
    [self.shareButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(49,49));
        make.right.mas_equalTo(self.toolView);
        make.top.mas_equalTo(self.toolLine.mas_bottom);
    }];
    [self.commentBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.toolView).offset(15);
        make.right.mas_equalTo(self.shareButton.mas_left).offset(-15);
        make.centerY.mas_equalTo(self.shareButton);
        make.height.mas_equalTo(@33);
    }];

    
    [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(self.contentView);
        make.height.mas_equalTo(XMHeight(64));
    }];
    
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.headerView).offset(15);
        make.right.mas_equalTo(self.headerView).offset(-15);
        make.bottom.mas_equalTo(self.headerView);
        make.height.mas_equalTo(@0.5);
    }];
    [self.cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.centerY.mas_equalTo(self.headerView);
        make.top.mas_equalTo(self.headerView).offset(10);
        make.size.mas_equalTo(CGSizeMake(44, 44));
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.cancelButton);
        make.centerX.mas_equalTo(self.headerView);
    }];
    
    
    CGFloat mainCommentH = [self.comment.Content xm_sizeWithFont:kCommentFont maxW:self.width - 80].height;
    CGFloat commentH = 50 + mainCommentH + 40;
    [self.mainComment mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.headerView.mas_bottom);
        make.right.left.mas_equalTo(self.contentView);
        make.height.mas_equalTo(commentH);
    }];
    
    [self.subTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentView).offset(15);
        make.top.mas_equalTo(self.mainComment.mas_bottom);
    }];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.contentView);
        make.bottom.mas_equalTo(self.toolView.mas_top);
        make.top.mas_equalTo(self.subTitleLabel.mas_bottom).offset(10);
    }];
    
    
}

- (UITableView *)tableView{
    if (!_tableView) {
        UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.tableFooterView = [UIView new];
        _tableView = tableView;
    }
    return _tableView;
}

- (ZInputToolbar *)inputToolbar{
    if (!_inputToolbar) {
        _inputToolbar = [[ZInputToolbar alloc] initWithFrame:CGRectMake(0,self.height, self.width, 49)];
        _inputToolbar.textViewMaxLine = 5;
        _inputToolbar.delegate = self;
        _inputToolbar.placeholderLabel.text = @"优质评论将会被优先展示";
        [self addSubview:_inputToolbar];
    }
    return _inputToolbar;
}
-(NSMutableDictionary *)commentDict{
    if (!_commentDict) {
        _commentDict = [NSMutableDictionary dictionary];
        [_commentDict setObject:@([UserAccountTool account].UserId) forKey:@"UserID"];
    }
    return _commentDict;
}
-(void)dealloc{
    NSLog(@"MoreReplyView --- dealloc ");
}


@end

